<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\App\BaseController;
use App\Models\Admin;
use App\Models\Department;
use App\Models\User;
use App\Models\Role;

class AdminController extends BaseController
{

    public function test(){
        dd(\Route::currentRouteName());
        echo 123;
    }
    /**
     * 管理员列表
     * @return [type] [description]
     */
    public function adminList(Request $request) 
    {

        //管理员名称
        $name       = $request->input('name');
        //管理员部门
        $department = $request->input('department');
        //管理员等级
        $level      = $request->input('level');

        $status      = $request->input('status');

        $adminModel = new Admin();
        
        if ($name != null)          $adminModel = $adminModel->where('name', 'like', '%'.$name.'%');
        
        if ($department != null)    $adminModel = $adminModel->where('department', $department);
        
        if ($level != null)         $adminModel = $adminModel->where('level', $level);

        if ($status != null)        $adminModel = $adminModel->where('status', $status);

        $adminData  = $adminModel->orderBy('displayorder','asc')->paginate($this->pageSize)->toArray();

        //获得部门信息
        $department = Department::getDepartment();

        foreach ($adminData['data'] as &$value) {
            $name = isset($department[$value['department']]) ? $department[$value['department']] : '';
            $value['department_name'] =  $name;
        }

        return self::json($adminData);
    }

    /**
     * [获得单个管理员数据]
     * @return [type] [description]
     */
    public function adminOne(Request $request) 
    {

        $id = intval($request->input('id'));

        if ($id == 0) {

            return self::error('id有误');
        }

        $result = Admin::find($id);

        if (empty($result) ) {
            return self::error('id有误');
        }
        //获得部门角色
        //求交集
        $depart     = Department::find($result->department);
        $deparArr   = explode(',', $depart->roleids);
        $resultArr  = explode(',', $result->roleids);
        $roleArr    = array_intersect($deparArr, $resultArr);

        $roleids    = implode(',', $roleArr);
        
        $result['roles'] = Role::getRoleArr($roleids);

        return  self::json($result);

    }
    /**
     * 管理员添加
     * @return [type] [description]
     */
    public function adminAdd(Request $request) 
    {

        $params = [];
        $request->input('name')        == null || $params['name']        = $request->input('name');//管理员名称
        $request->input('login')       == null || $params['login']       = $request->input('login');//管理员登录帐号
        $request->input('password')    == null || $params['password']    = $request->input('password');//管理员登录密码
        $request->input('department')  == null || $params['department']  = $request->input('department'); //部门

        if (count($params) != 4) self::error('必填参数不能为空');

        $request->input('roleids')     == null || $params['roleids']     = $request->input('roleids');//角色ids字符串
        $request->input('avatar')      == null || $params['avatar']      = $request->input('avatar');


        $params['displayorder']    = $request->input('displayorder',0);
        $params['status']          = $request->input('status', 0);
        $params['level']           = $request->input('level', 2);
        //密码加密
        $params['password'] = User::create_pwd($params['password']);

        $adminModel = new Admin();
        //对象赋值
        $adminModel->setAttributes($params);

        $result = $adminModel->save();

        return $result ? self::success('添加成功') : self::error('添加失败');
    }

    /**
     * 管理员数据编辑
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function adminEdit(Request $request) 
    {

        $params = [];
        $request->input('name')         == null || $params['name']          = $request->input('name');//管理员名称
        $request->input('login')        == null || $params['login']         = $request->input('login');//管理员登录帐号
        $request->input('password')     == null || $params['password']      = $request->input('password');//管理员登录密码
        $request->input('department')   == null || $params['department']    = $request->input('department'); //部门
        $request->input('status')       == null || $params['status']        = $request->input('status');
        $request->input('roleids')      == null || $params['roleids']       = $request->input('roleids');
        $request->input('displayorder') == null || $params['displayorder']  = $request->input('displayorder');
        $request->input('avatar')       == null || $params['avatar']        = $request->input('avatar');
        
        if (isset($params['password']) == true) {
            //密码加密
            $params['password'] = User::create_pwd($params['password']);
        }
        
        $id = intval($request->input('id'));

        if ($id == 0) {

            return self::error('id有误');
        }

        if (count($params) == 0) return self::success('更新成功');

        $adminModel = Admin::find($id);

        if(empty($adminModel)) {

            return self::error('id有误');
        }

        $adminModel->setAttributes($params);

        $result = $adminModel->save();

        return $result ? self::success('更新成功') : self::error('更新失败');
    }

    /**
     * 管理员删除
     * @return [type] [description]
     */
    public function adminDelete(Request $request) 
    {
        
        $id = intval($request->input('id'));

        if ($id == 0) {

            return self::error('id有误');
        }

        $adminModel = Admin::find($id);

        if(empty($adminModel)) {

            return self::error('id有误');
        }

        $adminModel->isdelete = Admin::IS_DELETE;

        $result = $adminModel->save();

        return $result ? self::success('删除成功') : self::error('删除失败');
    }

    /**
     * 获得管理员角色
     * @return [type] [description]
     */
    public function adminRoleLook(Request $request) 
    {

        $id = intval($request->input('id'));

        if ($id == 0) {
            return self::error('id有误');
        }

        $adminModel = Admin::find($id);

        if(empty($adminModel)) {
            return self::error('id有误');
        }
        //根据ids字符串获得对应的角色
        $roles = Role::getRoleObj($adminModel->roleids);

        $rolesArr = [];
        foreach ($roles as $key => $value) {
            $rolesArr[$key]['name'] = $value->name;
            $rolesArr[$key]['id']   = $value->id;
        }
        return self::json($rolesArr);
    }

    /**
     * 管理员角色列表
     * @return [type] [description]
     */
    public function adminRoleList(Request $request) 
    {

        $id = intval($request->input('id'));

        if ($id == 0) {
            return self::error('id有误');
        }

        $adminModel = Admin::find($id);

        if(empty($adminModel)) {
            return self::error('id有误');
        }
        //获得管理员角色
        $adminRoles = explode(',',$adminModel->roleids);

        //获得部门的角色字符串
        $depart = Department::find($adminModel->department);

        //根据ids字符串获得对应的角色
        $roles = Role::getRoleObj($depart->roleids);

        $data = [];

        foreach ($roles as $key => $val) {
 
            $data[$key]['id']    = $val->id;
            $data[$key]['name']  = $val->name;
            $data[$key]['check'] = in_array($val->id, $adminRoles) ? true : false;
        }

        return self::json($data);
    }

    /**
     * 角色编辑
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function adminRoleEdit(Request $request) 
    {

        $id      = intval($request->input('id'));
        $roleids = $request->input('roleids');

        if ($id == 0 || $roleids == null) {
            return self::error('参数有误');
        }

        $adminModel = Admin::find($id);

        if(empty($adminModel)) {
            return self::error('id有误');
        }

        $adminModel->roleids = $roleids;
        $result = $adminModel->save();

        return $result ? self::success('管理员角色编辑成功') : self::error('管理员角色编辑失败');
    }
}
